<?php
namespace Common\ORG\Qy;

/**
 * 标签管理
 * 
*/

use \Common\ORG\Tools\Http;

class Tag extends Base{
    const URL_CREATE = 'https://qyapi.weixin.qq.com/cgi-bin/tag/create';
    const URL_UPDATE = 'https://qyapi.weixin.qq.com/cgi-bin/tag/update';
    const URL_DELETE = 'https://qyapi.weixin.qq.com/cgi-bin/tag/delete';
    const URL_GET = 'https://qyapi.weixin.qq.com/cgi-bin/tag/get';
    const URL_GET_USER = 'https://qyapi.weixin.qq.com/cgi-bin/tag/get';
    const URL_ADD_USER = 'https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers';
    const URL_DEL_USER = 'https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers';
    const URL_QUERY = 'https://qyapi.weixin.qq.com/cgi-bin/tag/list';
    
    //初始化函数
    public function __construct($token)
    {
        $this->_token = $token;
    }

    /**
     * 创建标签
     * 
    */
    public function create($tagname, $tagid = null)
    {
        $ret = $this->m_ret;
        $url = self::URL_CREATE.'?access_token='.urlencode($this->_token);
        $data = array('tagname'=>$tagname);
        if ($tagid)  $data['tagid'] = $tagid;
        $param = $this->_param($data);
        $res = Http::post($url, $param);
        $crackData = $this->crackRes( $res );
        $ret['rcode'] = $crackData['rcode'];
        $ret['msg'] = $crackData['rcode'] ? 'OK':$crackData['msg'];
        $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
        return $ret;
    }

    /**
     * 更新标签名字
     * 
    */
    public function update($tagname, $tagid)
    {
        $ret = $this->m_ret;
        $url = self::URL_UPDATE.'?access_token='.urldecode( $this->_token);
        $data = array('tagname'=>$tagname, 'tagid'=>$tagid);
        $param = $this->_param($data);
        $str = http::post($url, $param);
        $crackData = $this->crackRes( $str );
        $ret['rcode'] = $crackData['rcode'];
        $ret['msg'] = $crackData['rcode'] ? 'OK':$crackData['msg'];
        $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
        return $ret;
    }

    /**
     * 删除标签
     * 
    */
    public function delete( $tagid )
    {
        $ret = $this->m_ret;
        if (!$tagid){
            $ret['msg'] = '参数错误';
        }else{
            $param = array();
            $param['access_token'] = $this->_token;
            $param['tagid'] = $tagid;
            $url = self::URL_DELETE.'?'.http_build_query( $param );
            $str = http::get( $url );
            $crackData = $this->crackRes( $str );
            $ret['rcode'] = $crackData['rcode'];
            $ret['msg'] = $crackData['rcode'] ? 'OK': $crackData['msg'];
            $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
        }
        return $ret;
    }

    /**
     * 获取标签成员
     * 
    */
    public function get( $tagid )
    {
        $ret = $this->m_ret;
        if (!$tagid){
            $ret['msg'] = '参数错误';
        }else{
            $param = array();
            $param['access_token'] = $this->_token;
            $param['tagid'] = $tagid;
            $url = self::URL_GET.'?'.http_build_query( $param );
            $str = http::get( $url );
            $crackData = $this->crackRes( $str );
            $ret['rcode'] = $crackData['rcode'];
            if ($crackData['rcode']&&isset($crackData['data'])){
                $ret['msg'] = 'OK';
                $ret['data'] = isset($crackData['data']['userlist']) ? $crackData['data']['userlist']:false;
            }else
                $ret['msg'] = $crackData['msg'];
        }
        return $ret;
    }

    /**
     * 增加标签成员
     * 
    */
    public function addUser( $tagid, $userlist = null, $partylist = null )
    {
        $ret = array('rcode'=>0,'msg'=>'','invalidlist'=>null,'partylist'=>null);
        if (!$tagid){
            $ret['msg'] = '参数错误';
        }else{
            $data = array('tagid'=>$tagid);
            if ($userlist)    $data['userlist'] = $userlist;
            if ($partylist)    $data['partylist'] = $partylist;
            if (!$userlist && !$partylist){
                $ret['msg'] = '企业成员ID列表与企业部门ID列表不能同时为空';
                return $ret;
            }
            $url = self::URL_ADD_USER.'?access_token='.urlencode( $this->_token );
            $param = $this->_param($data);
            $str = http::post( $url, $param );
            $crackData = $this->crackRes( $str );
            $ret['rcode'] = $crackData['rcode'];
            $ret['msg'] = $crackData['rcode'] ? 'OK': $crackData['msg'];
            if (!$ret['rcode']){
                $ret['invalidlist'] = $crackData['data']['invalidlist'];
                $ret['partylist'] = $crackData['data']['partylist'];
            }
        }
        return $ret;
    }

    /**
     * 删除标签成员
     * 
    */
    public function delUser( $tagid, $userlist = null, $partylist = null )
    {
        $ret = array('rcode'=>0,'msg'=>'','invalidlist'=>null,'partylist'=>null);
        if (!$tagid){
            $ret['msg'] = '参数错误';
        }else{

            $data = array('tagid'=>$tagid);
            if ($userlist)    $data['userlist'] = $userlist;
            if ($partylist)    $data['partylist'] = $partylist;
            if (!$userlist && !$partylist){
                $ret['msg'] = '企业成员ID列表与企业部门ID列表不能同时为空';
                return $ret;
            }
            $url = self::URL_DEL_USER.'?access_token='.urlencode( $this->_token );
            $param = $this->_param($data);
            $str = http::post( $url, $param );
            $crackData = $this->crackRes( $str );
            $ret['rcode'] = $crackData['rcode'];
            $ret['msg'] = $crackData['rcode'] ? 'OK': $crackData['msg'];
            if (!$ret['rcode']){
                $ret['invalidlist'] = $crackData['data']['invalidlist'];
                $ret['partylist'] = $crackData['data']['partylist'];
            }
        }
        return $ret;
    }

    /**
     * 查询标签
     * 
    */
    public function query()
    {
        $ret = $this->m_ret;
        $url = self::URL_QUERY.'?access_token='.urlencode($this->_token);
        $str = http::get($url);
        $crackData = $this->crackRes( $str );
        $ret['rcode'] = $crackData['rcode'];
        $ret['msg'] = $ret['rcode'] ? 'OK':$crackData['msg'];
        if ($ret['rcode'])
            $ret['data'] = $crackData['data']['taglist'];
        return $ret;
    }
}

?>